clear all
set more off


forvalues i=1/14{
	
	local alpha`i'=``i''
}

local beta = `15'
local sigma = `16'

local J = `17'
local N = `18'
local dgp = `19'
set obs `N'



if(`dgp' == 1) {
 gen c = exp(5+50*rnormal())

}



if(`dgp' == 2) {
*Now c is the threshold utility at which you stop searching
 gen c = exp(1+10*rnormal())
}

if(`dgp' == 3) {
*Now you search if free utility > c
 gen c = 3+9*rnormal()

}

if(`dgp' == 4) {
*Randomly search up to c goods
 gen c = `J'
}



forvalues i=1/14{
gen alpha`i'=`alpha`i''	
}

gen beta = `beta'

gen ID = _n
do programs/makedatasetup.do `J' 
reshape long epsilon_ , i(ID c beta) j(altID)

rename epsilon_ epsilon

merge 1:1 ID altID using realxz
replace z=z*`sigma'
drop chosen _merge



*DGP index
*1 == Weitzman
*2 == Satisficing
*3 == Search all goods above a threshold
*4 == Random search

if(`dgp' == 1) {
do programs/makesimsetup.do `J' `sigma' `sigmax'
}


if(`dgp' == 2) {
do programs/makesimsetupsatisfice.do `J' `sigma' `sigmax'
}


if(`dgp' == 3) {
do programs/makesimsetupthreshold.do `J' `sigma' `sigmax'
}

if(`dgp' == 4) {
do programs/makesimsetuprandomstop.do `J' `sigma' `sigmax'
}



*limited z variation method

preserve 
bysort ID:egen stdz=sd(z)
sum stdz 
local stdzfull=r(mean)
keep ID stdz
duplicates drop
xtile zdecile=stdz,nq(10)
tempfile zdecile 
save `zdecile',replace
restore 

merge m:1 ID using `zdecile'
drop _merge

preserve
 keep if zdecile==1 
 clogit chosen x* z ,group(ID) 
 
sum stdz 
local stdzdecile=r(mean)
 local alpha1 = _b[x1]
 local alpha1sd=_se[x1]

restore

drop zdecile



keep if _n == 1
keep chosen
gen dgp = `dgp'
gen J = `J'
gen sigma = `sigma'
gen alpha=`alpha1'

drop chosen



